package br.com.dwii.tpg2.entidade;

import br.com.dwii.tpg2.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Diego
 */
public class Usuario {
    
    private Long id;
    private Long idCaracteristica;
    private String nome;
    private String cpf;
    private String fone;
    private String email;
    private String senha;
    private Caracteristica carac;

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFone() {
        return fone;
    }

    public void setFone(String fone) {
        this.fone = fone;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getIdCaracteristica() {
        return idCaracteristica;
    }

    public void setIdCaracteristica(Long idCaracteristica) {
        this.idCaracteristica = idCaracteristica;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public Caracteristica getCarac() {
        return carac;
    }

    public void setCarac(Caracteristica carac) {
        this.carac = carac;
    }
    
    public Long save() throws SQLException {
        Connection conn = DBUtil.getConnection();
        PreparedStatement ps = null;
        Long ret = 0L;
        try {
            String sql = "insert into usuario (id_caracteristica, nome, cpf, fone, email, senha) values(?, ?, ?, ?, ?, ?)";
            ps = conn.prepareStatement(sql);
            ps.setLong(1, this.idCaracteristica);
            ps.setString(2, this.nome.toUpperCase());
            ps.setString(3, this.cpf);
            ps.setString(4, this.fone);
            ps.setString(5, this.email.toLowerCase());
            ps.setString(6, this.senha);
            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(Usuario.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        } finally {
            ps.close();
            conn.close();
        }
        return ret;
    }
    
    public void login() throws SQLException {
        Connection conn = DBUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            StringBuilder sql = new StringBuilder();
            sql.append("  select a.id, a.nome, a.cpf, a.fone, ");
            sql.append(" c.id, c.flag_tipo_animal, c.flag_sexo, c.flag_idade, c.flag_porte, ");
            sql.append(" c.flag_pelagem, c.flag_raca, c.flag_esterilizado, c.flag_vacinado, c.flag_vermifugado");
            sql.append("    from usuario a ");
            sql.append("       , caracteristica c ");
            sql.append("   where a.id_caracteristica = c.id ");
            sql.append("     and a.email = ? and a.senha = ?");
            ps = conn.prepareStatement(sql.toString());
            ps.setString(1, this.email);
            ps.setString(2, this.senha);
            rs = ps.executeQuery();
            while (rs.next()) {
                this.id = rs.getLong(1);
                this.nome = rs.getString(2);
                this.cpf = rs.getString(3);
                this.fone = rs.getString(4);
                
                this.carac = new Caracteristica();
                this.carac.setId(rs.getLong(5));
                this.carac.setFlagTipoAnimal(rs.getString(6));
                this.carac.setFlagSexo(rs.getString(7));
                this.carac.setFlagIdade(rs.getString(8));
                this.carac.setFlagPorte(rs.getString(9));
                this.carac.setFlagPelagem(rs.getString(10));
                this.carac.setFlagRaca(rs.getString(11));
                this.carac.setFlagEsterilizado(rs.getString(12));
                this.carac.setFlagVacinado(rs.getString(13));
                this.carac.setFlagVermifugado(rs.getString(14));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Usuario.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        } finally {
            ps.close();
            rs.close();
            conn.close();
        }
    }
}
